// formPackage/pages/article/newsList/newsList.js

import { queryNewsList, queryDynList, queryCommentNotRead, queryNewsListByTag } from "../../../../service/article_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    switchArr: [
      {
        name: "房友圈",
        key: 1,
        type: 99,
      },
      {
        name: "推荐",
        key: "",
        type: null,
      },
      {
        name: "头条",
        key: 2,
        type: null,
      },
      {
        name: "房价",
        key: 3,
        type: null,
      },
      {
        name: "商铺",
        key: 3,
        type: null,
      }
    ],
    topTypeIndex: 0,
    // RICH_TEXT(1, "富文本"),
    // MP(3, "公众号")
    newsList: [],
    dataList: [

    ],
    scrollRefreshStatus: true,

    page: 0,
    pageSize: 10,
    searchWords: "",
    newsTag: "",
    notReadList: [],
    isShowLoading: false,//加载状态出现
    isBottomShow: false,//是否没有更多数据了
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.articleTags && app.globalData.articleTags.length > 0) {
      this.setData({
        switchArr: app.globalData.articleTags
      })
    }
    //第一次进来默认查询房友圈数据
    this.queryDynList();
    //获取消息未读数量:
    this.queryCommentNotRead();
  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //顶部切换点击
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let name = e.currentTarget.dataset.name;
    this.setData({
      topTypeIndex: index
    })
    if (index != 0) {
      this.setData({
        newsTag: name,
        isBottomShow: false,
        page: 0,
      })
      this.queryNewsListByTag();
    } else {
      this.setData({
        isBottomShow: false,
        page: 0,
      })
      this.queryDynList();
    }
  },
  searchAction(e) {
    console.log("JJLog:e=" + JSON.stringify(e));
    let word = e.detail.searchWord;
    this.setData({
      searchWords: word
    })
    if (this.data.topTypeIndex == 0) {
      this.setData({
        isBottomShow: false,
        page: 0,
      })
      this.queryDynList();
    } else {
      this.setData({
        isBottomShow: false,
        page: 0,
      })
      this.queryNewsListByTag();
    }
  },

  //房友圈数据查询:
  queryDynList() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    queryDynList(pageNum, pageSize, this.data.searchWords).then(res => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false
      })
      console.log("JJLog:动态列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.commonListValueDeal(res.data, 'dyn', pageNum, pageSize)
      }
    }).catch(err => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false
      })
      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryDynList", err);
    })
  },
  //房友圈未读消息数量:
  queryCommentNotRead() {
    queryCommentNotRead(2).then(res => {
      wx.hideLoading();
      console.log("JJLog:房友圈未读数量:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          notReadList: res.data || [],
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:queryCommentNotRead", err);
    })
  },


  //查询新闻列表
  queryNewsListByTag() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    queryNewsListByTag(pageNum, pageSize, this.data.newsTag, this.data.searchWords).then(res => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false
      })
      console.log("JJLog:新闻列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.commonListValueDeal(res.data, "news", pageNum, pageSize);
      }
    }).catch(err => {
      this.setData({
        scrollRefreshStatus: false
      })
      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
      })
      console.error("JJLog:queryNewsListByTag", err);
    })
  },
  //公共的列表赋值方法:
  commonListValueDeal(list, type, pageNum, pageSize) {
    let tempArr = [];
    if (type == "dyn") {
      tempArr = this.data.dataList
    } else if (type == "news") {
      tempArr = this.data.newsList;
    }
    if (tempArr.length > 0 && pageNum > 0) {
      //加载下一页:
      tempArr = tempArr.concat(list);
      if (list.length < pageSize) {
        this.setData({
          isShowLoading: false,
          isBottomShow: true,
        })
        if (type == "dyn") {
          this.setData({
            dataList: tempArr
          })
        } else if (type == "news") {
          this.setData({
            newsList: tempArr
          })
        }

      } else if (list.length == pageSize) {
        this.setData({
          isShowLoading: false,
        })
        if (type == "dyn") {
          this.setData({
            dataList: tempArr
          })
        } else if (type == "news") {
          this.setData({
            newsList: tempArr
          })
        }
      }
    } else {
      this.setData({
        isShowLoading: false,
      })
      if (type == "dyn") {
        this.setData({
          dataList: list
        })
      } else if (type == "news") {
        this.setData({
          newsList: list
        })
      }
      if (list.length < pageSize) {
        this.setData({
          isBottomShow: true
        })
      }
    }
  },

  //跳转到发布动态列表
  pushPublishNews() {
    wx.navigateTo({
      url: '/formPackage/pages/article/publishArticle/publishArticle',
    })
  },

  //scroll滚动到底:
  scrolltolower() {
    if (this.data.isBottomShow) {
      return;
    }
    if (this.data.topTypeIndex == 0) {
      //房友圈
      let tempPage = this.data.page + 1;
      this.setData({
        page: tempPage,
        isShowLoading: true
      })
      this.queryDynList();

    } else {
      //新闻列表
      let tempPage = this.data.page + 1;
      this.setData({
        page: tempPage,
        isShowLoading: true
      })
      this.queryNewsListByTag();
    }

  },

  //scroll下拉刷新:
  refreshDownData() {
    if (this.data.topTypeIndex == 0) {
      //请求房友圈动态
      this.setData({
        isBottomShow: false,
        page: 0,
        scrollRefreshStatus: true,
      })
      this.queryDynList();
      this.queryCommentNotRead();
    } else {
      //请求文章:
      this.setData({
        isBottomShow: false,
        page: 0,
        scrollRefreshStatus: true,
      })
      this.queryNewsListByTag();
    }
  },





})